#!/bin/bash


Kis_cobbler_api_getdata="http://10.10.254.1:10001/kis_cobbler_api_getdata"
Kis_cobbler_api_postresult="http://10.10.254.1:10001/kis_cobbler_api_postresult"
#SN=$(/usr/sbin/dmidecode -t 1 | grep "Serial Number:" |awk -F ':' '{print $2}'|sed -e "s/^[ \s]\{1,\}//g" | sed -e "s/[ \s]\{1,\}$//g")
SN="hello"
Argment={\"sn\":\"$SN\"}
Retry_num=10
exit_flag=0
fail_mess=""

function int_value {
        eth0=`echo $return|awk '{split($2,a,"|");print a[2]}'`
        eth1=`echo $return|awk '{split($3,a,"|");print a[2]}'`
        hostname=`echo $return|awk '{split($4,a,"|");print a[2]}'`
        password=`echo $return|awk '{split($5,a,"|");print a[2]}'`
}

function hostname_config {
        if [ "$hostname" ];then
                HOSTNAME=$hostname
                hostname $hostname
                sed -i "/KIDC/d" /etc/hosts
                echo "127.0.0.1 $HOSTNAME" >> /etc/hosts
                sed -i "/HOSTNAME/d" /etc/sysconfig/network
                echo "HOSTNAME=$HOSTNAME" >> /etc/sysconfig/network
        else
                HOSTNAME="yw_0_0."$hostname
                hostname $hostname
                sed -i "/KIDC/d" /etc/hosts
                echo "127.0.0.1 $HOSTNAME" >> /etc/hosts
                sed -i "/HOSTNAME/d" /etc/sysconfig/network
                echo "HOSTNAME=$HOSTNAME" >> /etc/sysconfig/network
        fi
}

function eth0_config {
        if [ "$eth0" ];then
                ipaddr=`echo $eth0|awk -F '/' '{print $1}'`
                netmask=`echo $eth0|awk -F '/' '{print $2}'`
                sed -i 's/ONBOOT=no/ONBOOT=yes/g' /etc/sysconfig/network-scripts/ifcfg-eth0
                sed -i 's/\(BOOTPROTO=\).*/\1static/g' /etc/sysconfig/network-scripts/ifcfg-eth0
                sed -i '/IPADDR/d' /etc/sysconfig/network-scripts/ifcfg-eth0
                echo "IPADDR=$ipaddr" >> /etc/sysconfig/network-scripts/ifcfg-eth0
                sed -i '/NETMASK/d' /etc/sysconfig/network-scripts/ifcfg-eth0
                echo "NETMASK=$netmask" >> /etc/sysconfig/network-scripts/ifcfg-eth0
        fi
}

function eth1_config {
        if [ "$eth1" ];then
                ipaddr=`echo $eth1|awk -F '/' '{print $1}'`
                netmask=`echo $eth1|awk -F '/' '{print $2}'`
                sed -i 's/ONBOOT=no/ONBOOT=yes/g' /etc/sysconfig/network-scripts/ifcfg-eth1
                sed -i 's/\(BOOTPROTO=\).*/\1static/g' /etc/sysconfig/network-scripts/ifcfg-eth1
                sed -i '/IPADDR/d' /etc/sysconfig/network-scripts/ifcfg-eth1
                echo "IPADDR=$ipaddr" >> /etc/sysconfig/network-scripts/ifcfg-eth1
                sed -i '/NETMASK/d' /etc/sysconfig/network-scripts/ifcfg-eth1
                echo "NETMASK=$netmask" >> /etc/sysconfig/network-scripts/ifcfg-eth1
        fi
}

function gateway_config {
        if [ "$eth0" ];then
                ipaddr=`echo $eth0|awk -F '/' '{print $1}'`
                gateway=`echo $ipaddr | sed  's/\([0-9]*\.[0-9]*\.[0-9]*\.\).*/\11/g'`
                sed -i '/GATEWAY/d' /etc/sysconfig/network
                echo "GATEWAY=$gateway" >> /etc/sysconfig/network
        elif [ "$eth1" ];then
                ipaddr=`echo $eth1|awk -F '/' '{print $1}'`
                gateway=`echo $ipaddr | sed  's/\([0-9]*\.[0-9]*\.[0-9]*\.\).*/\11/g'`
                sed -i '/GATEWAY/d' /etc/sysconfig/network
                echo "GATEWAY=$gateway" >> /etc/sysconfig/network
        fi
}
function chpasswd_exc {
        password=$1
        openssl_exc=`which openssl`
        en_passwd=`$openssl_exc passwd -1 "$password"`
        if [ "$?" -eq 0 ];then
                echo "root:$en_passwd" | chpasswd -e 
                        if [ "$?" -ne 0 ];then
                                exit_flag=1
                                fail_mess="修改密码出错\n$fail_mess"
                        fi
        else
                exit_flag=1
                fail_mess="以下命令执行错误:$openssl_exc passwd -1 $password ,未修改密码\n$fail_mess"
        fi
}
function password_config {
    if [ "$password" ];then
        chpasswd_exc $password
        else
                exit_flag=1
                fail_mess="获取密码为空，修改为默认密码\n$fail_mess"
                chpasswd_exc "king+5688"
        fi
}

function post_result {
        if [ "$exit_flag" -eq 0 ];then
                Argment={\"sn\":\"$SN\",\"result\":"0",\"message\":\"配置成功完成\"}
                curl -d $Argment $Kis_cobbler_api_postresult --retry $Retry_num
        else
                Argment={\"sn\":\"$SN\",\"result\":"1",\"message\":\"$fail_mess\"}
                curl -d $Argment $Kis_cobbler_api_postresult --retry $Retry_num
        fi
}

return=`curl -d "$Argment" "$Kis_cobbler_api_getdata"  --retry $Retry_num`

check_op1=`echo $return|awk '{split($1,a,"|");print a[2]}'`
if [ "$check_op1" = "ok" ];then
        int_value
        hostname_config
        eth0_config
        eth1_config
        gateway_config
        password_config

else
        exit_flag=1
        fail_mess="$Kis_cobbler_api_getdata 调用API出错.\n$fail_mess"
fi

post_result
